/**
 * $Id: SGIPDeliverMessage.java,v 1.1 2006/09/07 02:08:40 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.sgip12msg;

import com.haoxi.gateway.util.TypeConvert;
public class SGIPDeliverMessage extends SGIPMessage {

    public SGIPDeliverMessage(byte buf[]) throws IllegalArgumentException {
        int len = TypeConvert.byte2int(buf, 57);
        len = 69 + len;
        if (buf.length != len) {
            throw new IllegalArgumentException("The message that is sent by SMC is wrong");
        } else {
            super.buf = new byte[len];
            System.arraycopy(buf, 0, super.buf, 0, buf.length);
            decodeSeq();
        }
    }

    public String getUserNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 12, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public String getSPNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 33, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public int getTpPid() {
        int tmpId = buf[54];
        return tmpId;
    }

    public int getTpUdhi() {
        int tmpId = buf[55];
        return tmpId;
    }

    public int getMsgFmt() {
        int tmpFmt = buf[56];
        return tmpFmt;
    }

    public int getMsgLength() {
        return TypeConvert.byte2int(buf, 57);
    }

    public byte[] getMsgContent() {
        int len = getMsgLength();
        byte tmpContent[] = new byte[len];
        System.arraycopy(buf, 61, tmpContent, 0, len);
        return tmpContent;
    }

    public String getReserve() {
        int loc = 61 + getMsgLength();
        byte tmpReserve[] = new byte[8];
        System.arraycopy(buf, loc, tmpReserve, 0, 8);
        return (new String(tmpReserve)).trim();
    }

    public String toString() {
        String tmpStr= new StringBuffer("SGIP_DELIVER: ")
                .append("Src_node_Id=").append(getSrcNodeId())
                .append(",Time_Stamp=").append(getTimeStamp())
                .append(",Sequence_Id=").append(getSequenceId())
                .append(",UserNumber=").append(getUserNumber())
                .append(",SPNumber=").append(getSPNumber())
                .append(",TpPid=").append(getTpPid())
                .append(",TpUdhi=").append(getTpUdhi())
                .append(",MsgFmt=").append(getMsgFmt())
                .append(",MsgLength=").append(getMsgLength())
                .append(",MsgContent=").append(new String(getMsgContent()))
                .append(",Reserve=").append(getReserve()).toString();        
        return tmpStr;
    }
    public int getCommandId() {
        return SGIPCommon.SGIP_DELIVER;
    }
}
